package com.taotao.search.service.impl;

import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.SearchItem;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.search.mapper.SearchItemMapper;
import com.taotao.search.service.SearchItemService;
@Service
public class SearchItemServiceImpl implements SearchItemService{
	//注入dao
	@Autowired
	private SearchItemMapper searchItemMapper;
	//注入solrServe
	@Autowired
	private SolrServer solrServer;
	@Override
	public TaotaoResult importAllItemToIndex() throws Exception {
		List<SearchItem> itemList = searchItemMapper.getItemList();
		for (SearchItem searchItem : itemList) {
			//创建文档
			SolrInputDocument document = new SolrInputDocument();
			//为文档添加域
			document.addField("id", searchItem.getId());
			document.addField("item_title", searchItem.getTitle());
			document.addField("item_sell_point", searchItem.getSell_point());
			document.addField("item_price", searchItem.getPrice());
			document.addField("item_image", searchItem.getImage());
			document.addField("item_category_name", searchItem.getCategory_name());
			document.addField("item_desc", searchItem.getItem_desc());
			solrServer.add(document);
		}
		//提交
		solrServer.commit();
		//返回TaotaoResul
		return TaotaoResult.ok();
	}
}
